#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>

#define high 20
#define width 40
//函数外全局变量定义

int ball_x, ball_y;						//小球坐标
int ball_vx, ball_vy;					//小球速度
int position_x, position_y;				//挡板中心坐标
int ridus;								//挡板半径
int left, right;						//挡板左右位置
int score;								//得分
int canvas[high][width] = { 0 };		//0输出空格，1输出小球O，2输出挡板*,3输出方块

void gotoxy(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}

void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = { 1,0 };//第二个值为0表示隐藏光标
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void startup()						//数据初始化
{
	int i,j;

	//小球
	ball_x = 6;
	ball_y = width / 2;
	canvas[ball_x][ball_y] = 1;

	ball_vx = 1;
	ball_vy = 1;

	//挡板
	position_x = high - 3;
	position_y = width / 2;
	ridus = 5;
	left = position_y - ridus;
	right = position_y + ridus;
	for (i = left; i <= right; i++)
		canvas[position_x][i] = 2;

	//方块
	for (i = 0; i < 4; i++)
		for (j = 0; j < width - 1; j++)
			canvas[i][j] = 3;

	//得分
	score = 0;

	HideCursor(); //隐藏光标
}

void show()    //显示画面
{
	gotoxy(0, 0);					//光标移动到原点
	int i, j;
	for (i = 0; i < high; i++)
	{
		for (j = 0; j < width; j++)
		{
			if (canvas[i][j] == 1)
				printf("O");		//输出小球
			else if (i == high - 1)
				printf("-");		//下边框
			else if (j == width - 1)
				printf("|");		//右边框
			else if (canvas[i][j] == 2)
				printf("*");		//挡板
			else if (canvas[i][j] == 3)
				printf("#");		//方块
			else if (canvas[i][j] == 0)
				printf(" ");		//输出空格
		}
		printf("\n");
	}
	printf("score:%d\n", score);
}

void updateWithoutInput()		//与用户输入无关的更新
{
	//小球移动
	canvas[ball_x][ball_y] = 0;
	ball_x = ball_x + ball_vx;
	ball_y = ball_y + ball_vy;
	canvas[ball_x][ball_y] = 1;

	//小球反弹
	if (ball_x == position_x-1)
	{
		if (ball_y >= left && ball_y <= right)
			ball_vx = -ball_vx;
	}
	if (ball_x==0)
		ball_vx = -ball_vx;
	if (ball_y == width - 1 || ball_y == 0)
		ball_vy = -ball_vy;

	//击中方块得分
	if (canvas[ball_x - 1][ball_y] == 3)
	{
		canvas[ball_x - 1][ball_y] = 0;
		ball_vx = -ball_vx;
		score++;
	}
	if(canvas[ball_x][ball_y-1]==3)
	{
		canvas[ball_x][ball_y-1] = 0;
		ball_vy = -ball_vy;
		score++;
	}
	if (canvas[ball_x][ball_y + 1] == 3)
	{
		canvas[ball_x][ball_y + 1] = 0;
		ball_vy = -ball_vy;
		score++;
	}

	//游戏失败
	if (ball_x == high-1)
	{
		printf("Game Over!");
		exit(0);
	}

	Sleep(100);

}
void updateWithInput()			//与用户输入有关的更新
{
	int i;
	char input;
	if (kbhit())		//当按键时执行
	{
		input = getch();
		if (input == 'a')
		{
			if (left > 0)
			{
				for (i = left; i <= right; i++)
					canvas[position_x][i] = 0;
				position_y = position_y - 2;
				left = position_y - ridus;
				right = position_y + ridus;
				for (i = left; i <= right; i++)
					canvas[position_x][i] = 2;
			}
		}
		if (input == 'd')
		{
			if (right < width - 2)
			{
				for (i = left; i <= right; i++)
					canvas[position_x][i] = 0;
				position_y = position_y + 2;
				left = position_y - ridus;
				right = position_y + ridus;
				for (i = left; i <= right; i++)
					canvas[position_x][i] = 2;
			}
		}
	}
}

int main()
{
	startup();
	while (1)
	{
		show();					//显示画面
		updateWithoutInput();	//与用户输入无关的更新
		updateWithInput();		//与用户输入有关的更新
	}
	return 0;
}